//
//  LTPCacheManager.m
//  LPTClient
//
//  Created by mac on 2018/12/3.
//  Copyright © 2018年 zhaodd. All rights reserved.
//

#import "LTPCacheManager.h"
#import <YYKit/YYKit.h>
#import "NSObject+YYModel.h"

@interface LTPCacheManager ()

@property (nonatomic,strong) YYCache *diskCache;

@property (nonatomic,copy) NSString *userId;

@end

@implementation LTPCacheManager

- (instancetype)init
{
    self = [super init];
    if (self) {
        _diskCache = [[YYCache alloc] initWithName:@"user_info"];
    }
    return self;
}

static LTPCacheManager *cacheManager;
+ (instancetype)shareInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        cacheManager = [[LTPCacheManager alloc] init];
        
    });
    return cacheManager;
}

- (void)cacheUserInfo:(NSDictionary *)userDict
{
    NSString *userId = [NSString stringWithString:[[userDict valueForKey:@"doctor"] valueForKey:@"id"]];
    if (userId) {
        self.userId = userId;
        [self.diskCache setObject:userDict forKey:@"user_cache"];
    }
}

- (NSDictionary *)getUserInfo
{
    NSDictionary *dict = (NSDictionary*)[self.diskCache objectForKey:@"user_cache"];
    return dict;
}

- (UserInfoModel *)getUserInfoModel
{
    NSDictionary *dict = [self getUserInfo];
    UserInfoModel *model = [UserInfoModel modelWithDictionary:dict];
    return model;
}

- (void)logOut// 退出登录
{
    if (self.token) {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"LOGIN_TOKEN"];
    }
}

- (NSString *)token
{
    return [[NSUserDefaults standardUserDefaults] valueForKey:@"LOGIN_TOKEN"];
}

- (void)setToken:(NSString *)token
{
    [[NSUserDefaults standardUserDefaults] setValue:token forKey:@"LOGIN_TOKEN"];
}

- (BOOL)isCertified
{
    if (!self.getUserInfo) {
        // 需要登录获取登录信息
        return NO;
    }else {
        if (!self.getUserInfoModel) {
            // 需要登录获取登录信息
            return NO;
        }
        if (!self.getUserInfoModel.doctor) {
            return NO;
        }
        if (!self.getUserInfoModel.doctor.credential) {
            return NO;
        } else {
            UserInfoDetail *userInfoDetail = self.getUserInfoModel.doctor.credential;
            // 0 审核中  1 审核过   2 审核不过
            if (userInfoDetail.status == 1) {
                return YES;
            }
            return NO;
        }
    }
    return NO;
}

@end
